⚠️ Warning. You need at least 8GB RAM available to load the dataset
This is an optional step. You can skip it and install packages to your current environment.
python -m venv .venv/histtest
source .venv/histtest/bin/activate
pip install \
numpy==1.19.5 \
matplotlib==3.0.3 \
jupyter==1.0.0 \
pillow==5.4.1 \
scikit-image==0.14.2 \
mat73==0.55 \
himpy=0.0.1
# import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from himpy.histogram import operations
from himpy.executor import Parser, Evaluator
from himpy.utils import E
%load_ext autoreload
%autoreload 2
import sys
sys.path.insert(0, "../")
from utils.datasets import NYULoader
# feature extraction
from utils.feature_extraction import (
FeatureMerger,
PositionSetTransformer,
filter_data,
create_histogram,
create_histogram_,
extract_elements,
extract_element_set
)
from utils.feature_extraction.nyuv2 import (
NYUObjectSetTransformer,
NYUDepthSetTransformer,
load_nyu_histograms
)
# search engine
from utils.search_engine import SearchEngine
# plot
from utils.plot.matplotlib_plot import (
plot_position_grid,
plot_object_ids,
show_operation_result,
show_retrieved_images
)
Download the dataset from the NYU Depth Dataset V2 website:
Basic dataset fileds
| Key | Description |
|---|---|
| depths | HxWxN matrix of in-painted depth maps where H and W are the height and width, respectively and N is the number of images. The values of the depth elements are in meters. |
| images | HxWx3xN matrix of RGB images where H and W are the height and width, respectively, and N is the number of images. |
| instances | HxWxN matrix of instance maps. Use get_instance_masks.m in the Toolbox to recover masks for each object instance in a scene. |
| labels | HxWxN matrix of object label masks where H and W are the height and width, respectively and N is the number of images. The labels range from 1..C where C is the total number of classes. If a pixel’s label value is 0, then that pixel is ‘unlabeled’. |
| names | Cx1 cell array of the english names of each class. |
| namesToIds | map from english label names to class IDs (with C key-value pairs) |
| scenes | Nx1 cell array of the name of the scene from which each image was taken. |
| sceneTypes | Nx1 cell array of the scene type from which each image was taken. |
loader = NYULoader()
data = loader.fetch_load()
ERROR:root:ERROR: MATLAB type not supported: containers.Map, (uint32)
data.keys()
dict_keys(['accelData', 'depths', 'images', 'instances', 'labels', 'names', 'namesToIds', 'rawDepthFilenames', 'rawDepths', 'rawRgbFilenames', 'sceneTypes', 'scenes', 'idsToNames'])
# Id of some image from the dataset
IMAGE_INDX = 15
# Plot the image
I = data["images"][IMAGE_INDX]
plt.imshow(I)
plt.title("Image")
plt.show()
# Grid params: 5 splits along Y, and 5 along X
GRID = (5, 5)
# Create a position transformer
position_transformer = PositionSetTransformer(splits=GRID, element_ndim=3)
# Set an image size
# Note: Actually there is no fitting here, just follow the common interface
position_transformer.fit(X=I, y=None)
# Build an image in which each pixel defines a position
position_image = position_transformer.transform(X=I, batch=False)
# Plot the positional element along with the initial image
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].axis("off")
axes[1].set_title("Low-level position elements")
axes[1].imshow(position_image)
axes[1] = plot_position_grid(position_transformer, axes[1])
axes[1].axis("off")
axes[2].set_title("Matching image and elements")
axes[2].imshow(I)
axes[2] = plot_position_grid(position_transformer, axes[2])
axes[2].axis("off")
plt.show()
# Create an instance of query parser
parser = Parser()
# Definition of high-level positional elements
Ep_top = E("1+2+3+4+5+6+7+8+9+10")
Ep_bottom = E("16+17+18+19+20+21+22+23+24+25")
Ep_left = E("1+2+6+7+11+12+16+17+21+22")
Ep_right = E("4+5+9+10+14+15+19+20+24+25")
Ep_center = E("7+8+9+12+13+14+17+18+19")
Eps = [
("top", Ep_top),
("bottom", Ep_bottom),
("left", Ep_left),
("right", Ep_right),
("center", Ep_center)
]
# Sets of high-level positional elements (they will be used for the Evaluator below)
Eps_set = { name: parser.parse_set(Ep.value) for name, Ep in Eps}
Eps_set["center"]
{'12', '13', '14', '17', '18', '19', '7', '8', '9'}
# Plot a high-level element
Ep_set_ = Eps_set["center"]
elements_image = position_transformer.filter_elements(position_image, Ep_set_)
filtered_image = position_transformer.filter_data(position_image, I, Ep_set_)
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].axis("off")
axes[1].set_title("High-level position elements")
axes[1].imshow(elements_image)
axes[1] = plot_position_grid(position_transformer, axes[1], Ep_set_)
axes[1].axis("off")
axes[2].set_title("Matching image and elements")
axes[2].imshow(filtered_image)
axes[2] = plot_position_grid(position_transformer, axes[2], Ep_set_)
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)
plt.show()
print("Total number of classes: {}\n".format(len(data["names"])))
print("Classes: {}\n".format(", ".join(cl[0] for cl in data["names"])))
Total number of classes: 894 Classes: book, bottle, cabinet, ceiling, chair, cone, counter, dishwasher, faucet, fire extinguisher, floor, garbage bin, microwave, paper towel dispenser, paper, pot, refridgerator, stove burner, table, unknown, wall, bowl, magnet, sink, air vent, box, door knob, door, scissor, tape dispenser, telephone cord, telephone, track light, cork board, cup, desk, laptop, air duct, basket, camera, pipe, shelves, stacked chairs, styrofoam object, whiteboard, computer, keyboard, ladder, monitor, stand, bar, motion camera, projector screen, speaker, bag, clock, green screen, mantel, window, ball, hole puncher, light, manilla envelope, picture, mail shelf, printer, stapler, fax machine, folder, jar, magazine, ruler, cable modem, fan, file, hand sanitizer, paper rack, vase, air conditioner, blinds, flower, plant, sofa, stereo, books, exit sign, room divider, bookshelf, curtain, projector, modem, wire, water purifier, column, hooks, hanging hooks, pen, electrical outlet, doll, eraser, pencil holder, water carboy, mouse, cable rack, wire rack, flipboard, map, paper cutter, tape, thermostat, heater, circuit breaker box, paper towel, stamp, duster, poster case, whiteboard marker, ethernet jack, pillow, hair brush, makeup brush, mirror, shower curtain, toilet, toiletries bag, toothbrush holder, toothbrush, toothpaste, platter, rug, squeeze tube, shower cap, soap, towel rod, towel, bathtub, candle, tissue box, toilet paper, container, clothes, electric toothbrush, floor mat, lamp, drum, flower pot, banana, candlestick, shoe, stool, urn, earplugs, mailshelf, placemat, excercise ball, alarm clock, bed, night stand, deoderant, headphones, headboard, basketball hoop, foot rest, laundry basket, sock, football, mens suit, cable box, dresser, dvd player, shaver, television, contact lens solution bottle, drawer, remote control, cologne, stuffed animal, lint roller, tray, lock, purse, toy bottle, crate, vasoline, gift wrapping roll, wall decoration, hookah, radio, bicycle, pen box, mask, shorts, hat, hockey glove, hockey stick, vuvuzela, dvd, chessboard, suitcase, calculator, flashcard, staple remover, umbrella, bench, yoga mat, backpack, cd, sign, hangers, notebook, hanger, security camera, folders, clothing hanger, stairs, glass rack, saucer, tag, dolly, machine, trolly, shopping baskets, gate, bookrack, blackboard, coffee bag, coffee packet, hot water heater, muffins, napkin dispenser, plaque, plastic tub, plate, coffee machine, napkin holder, radiator, coffee grinder, oven, plant pot, scarf, spice rack, stove, tea kettle, napkin, bag of chips, bread, cutting board, dish brush, serving spoon, sponge, toaster, cooking pan, kitchen items, ladel, spatula, spice stand, trivet, knife rack, knife, baking dish, dish scrubber, drying rack, vessel, kichen towel, tin foil, kitchen utensil, utensil, blender, garbage bag, sink protector, box of ziplock bags, spice bottle, pitcher, pizza box, toaster oven, step stool, vegetable peeler, washing machine, can opener, can of food, paper towel holder, spoon stand, spoon, wooden kitchen utensils, bag of flour, fruit, sheet of metal, waffle maker, cake, cell phone, tv stand, tablecloth, wine glass, sculpture, wall stand, iphone, coke bottle, piano, wine rack, guitar, light switch, shirts in hanger, router, glass pot, cart, vacuum cleaner, bin, coins, hand sculpture, ipod, jersey, blanket, ironing board, pen stand, mens tie, glass baking dish, utensils, frying pan, shopping cart, plastic bowl, wooden container, onion, potato, jacket, dvds, surge protector, tumbler, broom, can, crock pot, person, salt shaker, wine bottle, apple, eye glasses, menorah, bicycle helmet, fire alarm, water fountain, humidifier, necklace, chandelier, barrel, chest, decanter, wooden utensils, globe, sheets, fork, napkin ring, gift wrapping, bed sheets, spot light, lighting track, cannister, coffee table, mortar and pestle, stack of plates, ottoman, server, salt container, utensil container, phone jack, switchbox, casserole dish, oven handle, whisk, dish cover, electric mixer, decorative platter, drawer handle, fireplace, stroller, bookend, table runner, typewriter, ashtray, key, suit jacket, range hood, cleaning wipes, six pack of beer, decorative plate, watch, balloon, ipad, coaster, whiteboard eraser, toy, toys basket, toy truck, classroom board, chart stand, picture of fish, plastic box, pencil, carton, walkie talkie, binder, coat hanger, filing shelves, plastic crate, plastic rack, plastic tray, flag, poster board, lunch bag, board, leg of a girl, file holder, chart, glass pane, cardboard tube, bassinet, toy car, toy shelf, toy bin, toys shelf, educational display, placard, soft toy group, soft toy, toy cube, toy cylinder, toy rectangle, toy triangle, bucket, chalkboard, game table, storage shelvesbooks, toy cuboid, toy tree, wooden toy, toy box, toy phone, toy sink, toyhouse, notecards, toy trucks, wall hand sanitizer dispenser, cap stand, music stereo, toys rack, display board, lid of jar, stacked bins boxes, stacked plastic racks, storage rack, roll of paper towels, cables, power surge, cardboard sheet, banister, show piece, pepper shaker, kitchen island, excercise equipment, treadmill, ornamental plant, piano bench, sheet music, grandfather clock, iron grill, pen holder, toy doll, globe stand, telescope, magazine holder, file container, paper holder, flower box, pyramid, desk mat, cordless phone, desk drawer, envelope, window frame, id card, file stand, paper weight, toy plane, money, papers, comforter, crib, doll house, toy chair, toy sofa, plastic chair, toy house, child carrier, cloth bag, cradle, baby chair, chart roll, toys box, railing, clothing dryer, clothing washer, laundry detergent jug, clothing detergent, bottle of soap, box of paper, trolley, hand sanitizer dispenser, soap holder, water dispenser, photo, water cooler, foosball table, crayon, hoola hoop, horse toy, plastic toy container, pool table, game system, pool sticks, console system, video game, pool ball, trampoline, tricycle, wii, furniture, alarm, toy table, ornamental item, copper vessel, stick, car, mezuza, toy cash register, lid, paper bundle, business cards, clipboard, flatbed scanner, paper tray, mouse pad, display case, tree sculpture, basketball, fiberglass case, framed certificate, cordless telephone, shofar, trophy, cleaner, cloth drying stand, electric box, furnace, piece of wood, wooden pillar, drying stand, cane, clothing drying rack, iron box, excercise machine, sheet, rope, sticks, wooden planks, toilet plunger, bar of soap, toilet bowl brush, light bulb, drain, faucet handle, nailclipper, shaving cream, rolled carpet, clothing iron, window cover, charger and wire, quilt, mattress, hair dryer, stones, pepper grinder, cat cage, dish rack, curtain rod, calendar, head phones, cd disc, head phone, usb drive, water heater, pan, tuna cans, baby gate, spoon sets, cans of cat food, cat, flower basket, fruit platter, grapefruit, kiwi, hand blender, knobs, vessels, cell phone charger, wire basket, tub of tupperware, candelabra, litter box, shovel, cat bed, door way, belt, surge protect, glass, console controller, shoe rack, door frame, computer disk, briefcase, mail tray, file pad, letter stand, plastic cup of coffee, glass box, ping pong ball, ping pong racket, ping pong table, tennis racket, ping pong racquet, xbox, electric toothbrush base, toilet brush, toiletries, razor, bottle of contact lens solution, contact lens case, cream, glass container, container of skin cream, soap dish, scale, soap stand, cactus, door window reflection, ceramic frog, incense candle, storage space, door lock, toilet paper holder, tissue, personal care liquid, shower head, shower knob, knob, cream tube, perfume box, perfume, back scrubber, door facing trimreflection, doorreflection, light switchreflection, medicine tube, wallet, soap tray, door curtain, shower pipe, face wash cream, flashlight, shower base, window shelf, shower hose, toothpaste holder, soap box, incense holder, conch shell, roll of toilet paper, shower tube, bottle of listerine, bottle of hand wash liquid, tea pot, lazy susan, avocado, fruit stand, fruitplate, oil container, package of water, bottle of liquid, door way arch, jug, bulb, bagel, bag of bagels, banana peel, bag of oreo, flask, collander, brick, torch, dog bowl, wooden plank, eggs, grill, dog, chimney, dog cage, orange plastic cap, glass set, vessel set, mellon, aluminium foil, orange, peach, tea coaster, butterfly sculpture, corkscrew, heating tray, food processor, corn, squash, watermellon, vegetables, celery, glass dish, hot dogs, plastic dish, vegetable, sticker, chapstick, sifter, fruit basket, glove, measuring cup, water filter, wine accessory, dishes, file box, ornamental pot, dog toy, salt and pepper, electrical kettle, kitchen container plastic, pineapple, suger jar, steamer, charger, mug holder, orange juicer, juicer, bag of hot dog buns, hamburger bun, mug hanger, bottle of ketchup, toy kitchen, food wrapped on a tray, kitchen utensils, oven mitt, bottle of comet, wooden utensil, decorative dish, handle, label, flask set, cooking pot cover, tupperware, garlic, tissue roll, lemon, wine, decorative bottle, wire tray, tea cannister, clothing hamper, guitar case, wardrobe, boomerang, button, karate belts, medal, window seat, window box, necklace holder, beeper, webcam, fish tank, luggage, life jacket, shoelace, pen cup, eyeball plastic ball, toy pyramid, model boat, certificate, puppy toy, wire board, quill, canister, toy boat, antenna, bean bag, lint comb, travel bag, wall divider, toy chest, headband, luggage rack, bunk bed, lego, yarmulka, package of bedroom sheets, bedding package, comb, dollar bill, pig, storage bin, storage chest, slide, playpen, electronic drumset, ipod dock, microphone, music keyboard, music stand, microphone stand, album, kinect, inkwell, baseball, decorative bowl, book holder, toy horse, desser, toy apple, toy dog, scenary, drawer knob, shoe hanger, tent, figurine, soccer ball, hand weight, magic 8ball, bottle of perfume, sleeping bag, decoration item, envelopes, trinket, hand fan, sculpture of the chrysler building, sculpture of the eiffel tower, sculpture of the empire state building, jeans, garage door, case, rags, decorative item, toy stroller, shelf frame, cat house, can of beer, dog bed, lamp shade, bracelet, reflection of window shutters, decorative egg, indoor fountain, photo album, decorative candle, walkietalkie, serving dish, floor trim, mini display platform, american flag, vhs tapes, throw, newspapers, mantle, package of bottled water, serving platter, display platter, centerpiece, tea box, gold piece, wreathe, lectern, hammer, matchbox, pepper, yellow pepper, duck, eggplant, glass ware, sewing machine, rolled up rug, doily, coffee pot, torah
# Create object transformer
object_transformer = NYUObjectSetTransformer(data)
# Create a mask of objects
object_image = object_transformer.fit_transform(X=None, y=None, ids=IMAGE_INDX)
# Mask image based on segments
image__object_filtered = object_transformer.filter_data(object_image, I)
# Plot initial image, object image, and filtered image
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].axis("off")
axes[1].set_title("Low-level object elements")
axes[1].imshow(object_image)
axes[1] = plot_object_ids(object_image, axes[1])
axes[1].axis("off")
axes[2].set_title("Matching image and elements")
axes[2].imshow(image__object_filtered)
axes[2] = plot_object_ids(object_image, axes[2])
axes[2].axis("off")
plt.show()
# Names of image objects
object_transformer.get_names(extract_elements(object_image))
[('cabinet', 3),
('ceiling', 4),
('floor', 11),
('table', 19),
('wall', 21),
('tape dispenser', 30),
('light', 62),
('printer', 66),
('stapler', 67),
('books', 85),
('room divider', 87),
('bookshelf', 88)]
# Definition of high-level positional elements
Eo_room = E("4+11+21")
Eo_table = E("19")
Eo_printer = E("66")
Eo_ceiling = E("4")
Eo_door = E("28")
Eos = [
("room", Eo_room),
("table", Eo_table),
("printer", Eo_printer),
("ceiling", Eo_ceiling),
("door", Eo_door),
]
# Sets of high-level positional elements (they will be used for the Evaluator below)
Eos_set = { name: parser.parse_set(Eo.value) for name, Eo in Eos}
Eos_set["room"]
{'11', '21', '4'}
# Plot a high-level element
Eo_set_ = Eos_set["room"]
elements_image = object_transformer.filter_elements(object_image, element_ids=Eo_set_)
filtered_image = object_transformer.filter_data(object_image, I, element_ids=Eo_set_)
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].axis("off")
axes[1].set_title("High-level object elements")
axes[1].imshow(elements_image)
axes[1] = plot_object_ids(object_image, axes[1], Eo_set_)
axes[1].axis("off")
axes[2].set_title("Matching image and elements")
axes[2].imshow(filtered_image)
axes[2] = plot_object_ids(object_image, axes[2], Eo_set_)
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)
plt.show()
# Names of image objects
object_transformer.get_names(Eo_set_, return_id=True)
[('wall', '21'), ('ceiling', '4'), ('floor', '11')]
# Create object transformer
depth_transformer = NYUDepthSetTransformer(data, nbins=10)
# Create a mask of objects
depth_image = depth_transformer.fit_transform(X=None, y=None, ids=IMAGE_INDX)
# Mask image based on segments
image__depth_filtered = depth_transformer.filter_data(depth_image, I)
# Plot initial image, object image, and filtered image
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].axis("off")
axes[1].set_title("Low-level object elements")
axes[1].imshow(depth_image)
axes[1] = plot_object_ids(depth_image, axes[1])
axes[1].axis("off")
axes[2].set_title("Matching image and elements")
axes[2].imshow(image__depth_filtered)
axes[2].imshow(depth_image, alpha=0.4)
axes[2] = plot_object_ids(depth_image, axes[2])
axes[2].axis("off")
plt.show()
# Definition of high-level positional elements
Ed_faraway = E("7+8+9+10")
Ed_notfar = E("3+4+5+6")
Ed_near = E("1+2")
Eds = [
("faraway", Ed_faraway),
("notfar", Ed_notfar),
("near", Ed_near),
]
# Sets of high-level positional elements (they will be used for the Evaluator below)
Eds_set = { name: parser.parse_set(Ed.value) for name, Ed in Eds}
Eds_set["faraway"]
{'10', '7', '8', '9'}
# Plot a high-level element
Ed_set_ = Eds_set["faraway"]
elements_image = depth_transformer.filter_elements(depth_image, element_ids=Ed_set_)
filtered_image = depth_transformer.filter_data(depth_image, I, element_ids=Ed_set_)
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].axis("off")
axes[1].set_title("High-level object elements")
axes[1].imshow(elements_image)
axes[1] = plot_object_ids(depth_image, axes[1], Ed_set_)
axes[1].axis("off")
axes[2].set_title("Matching image and elements")
axes[2].imshow(filtered_image)
axes[2] = plot_object_ids(elements_image, axes[2], Ed_set_)
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)
plt.show()
# Option 1
hist = create_histogram((position_image, depth_image, object_image))
hist.to_dict()
{('1', '2', '4'): 0.029739583333333333,
('1', '3', '4'): 0.00302734375,
('1', '3', '62'): 0.0020963541666666665,
('2', '2', '4'): 0.029990234375,
('2', '3', '4'): 0.0069140625,
('2', '3', '21'): 0.00012044270833333334,
('3', '1', '4'): 0.0009049479166666666,
('3', '1', '21'): 6.184895833333334e-05,
('3', '2', '4'): 0.015315755208333334,
('3', '2', '21'): 0.018649088541666668,
('3', '3', '4'): 3.255208333333333e-05,
('3', '3', '21'): 0.00029296875,
('4', '1', '4'): 0.008655598958333333,
('4', '1', '21'): 0.00861328125,
('4', '1', '85'): 0.001435546875,
('4', '1', '88'): 0.0019270833333333334,
('4', '2', '4'): 0.007486979166666667,
('4', '2', '21'): 0.00020182291666666667,
('4', '2', '85'): 0.0020963541666666665,
('4', '2', '88'): 0.004651692708333334,
('5', '1', '21'): 0.035494791666666664,
('6', '3', '4'): 0.010615234375,
('6', '3', '62'): 0.006214192708333333,
('6', '4', '4'): 0.006484375,
('6', '4', '62'): 0.0044986979166666665,
('6', '5', '4'): 0.0074251302083333335,
('6', '5', '62'): 0.00078125,
('6', '6', '4'): 0.00025390625,
('7', '3', '4'): 0.014518229166666667,
('7', '3', '21'): 0.005657552083333333,
('7', '4', '4'): 0.009993489583333333,
('7', '4', '21'): 0.0027994791666666667,
('7', '4', '88'): 0.0003938802083333333,
('7', '5', '4'): 0.004117838541666667,
('7', '5', '21'): 0.00027018229166666667,
('7', '6', '4'): 0.0005924479166666667,
('8', '2', '4'): 0.007776692708333333,
('8', '2', '21'): 0.0026041666666666665,
('8', '2', '85'): 0.003994140625,
('8', '2', '88'): 0.008134765625,
('8', '3', '4'): 0.004593098958333333,
('8', '3', '21'): 0.000732421875,
('8', '3', '85'): 0.0032649739583333333,
('8', '3', '88'): 0.00705078125,
('8', '4', '4'): 2.2786458333333334e-05,
('8', '4', '88'): 0.00024739583333333335,
('9', '1', '21'): 0.008873697916666666,
('9', '1', '85'): 0.003404947916666667,
('9', '1', '88'): 0.00193359375,
('9', '2', '85'): 0.013567708333333333,
('9', '2', '88'): 0.011643880208333334,
('10', '1', '21'): 0.038125,
('11', '4', '87'): 0.0003385416666666667,
('11', '5', '87'): 0.00034505208333333333,
('11', '6', '4'): 0.00042317708333333335,
('11', '6', '21'): 0.008740234375,
('11', '6', '87'): 1.953125e-05,
('11', '7', '21'): 3.2552083333333335e-06,
('11', '8', '21'): 0.0020247395833333333,
('11', '9', '21'): 9.440104166666666e-05,
('12', '4', '4'): 0.0005143229166666666,
('12', '4', '21'): 9.114583333333334e-05,
('12', '4', '88'): 0.009567057291666667,
('12', '5', '4'): 0.00021158854166666667,
('12', '5', '21'): 0.008707682291666666,
('12', '5', '88'): 0.0018359375,
('12', '6', '4'): 0.001083984375,
('12', '6', '21'): 0.00291015625,
('12', '7', '21'): 4.557291666666667e-05,
('12', '8', '21'): 0.003203125,
('12', '9', '21'): 0.00012369791666666668,
('13', '2', '85'): 0.013180338541666666,
('13', '2', '88'): 0.0019010416666666668,
('13', '3', '85'): 0.010133463541666667,
('13', '3', '88'): 0.01376953125,
('13', '4', '88'): 0.001015625,
('14', '1', '21'): 0.008395182291666666,
('14', '1', '85'): 0.003544921875,
('14', '1', '88'): 0.0014485677083333334,
('14', '2', '85'): 0.018606770833333335,
('14', '2', '88'): 0.0072591145833333336,
('15', '1', '21'): 0.038125,
('16', '3', '87'): 0.0017057291666666666,
('16', '4', '87'): 0.0038313802083333333,
('16', '5', '11'): 1.6276041666666666e-05,
('16', '5', '87'): 0.0052669270833333335,
('16', '6', '11'): 0.000859375,
('16', '6', '21'): 0.007542317708333334,
('16', '6', '87'): 0.00032877604166666667,
('16', '7', '11'): 0.0016634114583333334,
('16', '7', '21'): 0.00021484375,
('16', '8', '11'): 7.161458333333333e-05,
('16', '8', '21'): 0.001474609375,
('17', '2', '66'): 0.0033333333333333335,
('17', '3', '19'): 0.00011393229166666667,
('17', '3', '21'): 0.00033203125,
('17', '3', '66'): 0.009674479166666666,
('17', '4', '19'): 3.580729166666667e-05,
('17', '4', '21'): 0.00034505208333333333,
('17', '4', '66'): 0.00017903645833333332,
('17', '4', '88'): 0.0006022135416666667,
('17', '5', '11'): 6.184895833333334e-05,
('17', '5', '19'): 3.2552083333333335e-06,
('17', '5', '21'): 0.004583333333333333,
('17', '5', '66'): 8.138020833333333e-05,
('17', '5', '88'): 0.00011393229166666667,
('17', '6', '11'): 0.002151692708333333,
('17', '7', '11'): 0.0029622395833333332,
('17', '7', '21'): 0.0005338541666666666,
('17', '8', '11'): 0.001959635416666667,
('17', '8', '21'): 0.0022819010416666665,
('17', '9', '11'): 0.000546875,
('18', '2', '21'): 0.001015625,
('18', '2', '66'): 0.026809895833333333,
('18', '2', '85'): 0.0008365885416666667,
('18', '2', '88'): 0.00181640625,
('18', '3', '21'): 0.0035286458333333333,
('18', '3', '66'): 0.001005859375,
('18', '3', '85'): 0.00010416666666666667,
('18', '3', '88'): 0.00111328125,
('18', '4', '66'): 8.138020833333333e-05,
('18', '4', '88'): 3.255208333333333e-05,
('19', '1', '21'): 0.010162760416666666,
('19', '1', '67'): 0.000615234375,
('19', '1', '85'): 0.0007975260416666667,
('19', '1', '88'): 0.0021126302083333335,
('19', '2', '21'): 0.014156901041666666,
('19', '2', '67'): 0.0002669270833333333,
('19', '2', '85'): 0.0022135416666666666,
('19', '2', '88'): 0.004134114583333333,
('20', '1', '21'): 0.038125,
('21', '3', '11'): 0.007161458333333333,
('21', '3', '87'): 0.0057161458333333335,
('21', '4', '11'): 0.010299479166666667,
('21', '4', '87'): 0.0011783854166666668,
('21', '5', '11'): 0.006149088541666667,
('21', '5', '87'): 0.002936197916666667,
('21', '6', '11'): 0.00060546875,
('22', '2', '3'): 0.005546875,
('22', '2', '11'): 6.510416666666667e-06,
('22', '2', '66'): 0.00021484375,
('22', '3', '3'): 0.0037044270833333334,
('22', '3', '11'): 0.005465494791666667,
('22', '3', '19'): 0.0018977864583333334,
('22', '3', '66'): 0.0005891927083333334,
('22', '4', '11'): 0.008958333333333334,
('22', '4', '19'): 0.0015169270833333332,
('22', '4', '21'): 3.580729166666667e-05,
('22', '5', '11'): 0.004593098958333333,
('22', '5', '19'): 0.00017252604166666667,
('22', '5', '21'): 0.000341796875,
('22', '6', '11'): 0.000908203125,
('23', '1', '3'): 6.184895833333334e-05,
('23', '1', '19'): 0.0025911458333333333,
('23', '2', '3'): 0.013990885416666666,
('23', '2', '19'): 0.005843098958333333,
('23', '2', '30'): 0.00064453125,
('23', '2', '66'): 0.00515625,
('23', '2', '67'): 0.004449869791666667,
('24', '1', '19'): 0.0017610677083333332,
('24', '1', '21'): 0.00720703125,
('24', '1', '67'): 0.010319010416666666,
('24', '2', '19'): 0.0002669270833333333,
('24', '2', '21'): 5.5338541666666664e-05,
('24', '2', '30'): 0.002509765625,
('24', '2', '67'): 0.00021809895833333333,
('25', '1', '21'): 0.036188151041666665}
# Option 2.a Merge features into a single image
feature_merger = FeatureMerger()
merged_image = feature_merger.fit_transform((position_image, depth_image, object_image))
merged_image
rec.array([[( 1, 2, 0), ( 1, 2, 0), ( 1, 2, 0), ..., ( 5, 1, 0),
( 5, 1, 0), ( 5, 1, 0)],
[( 1, 2, 0), ( 1, 2, 0), ( 1, 2, 0), ..., ( 5, 1, 0),
( 5, 1, 0), ( 5, 1, 0)],
[( 1, 2, 0), ( 1, 2, 0), ( 1, 2, 0), ..., ( 5, 1, 0),
( 5, 1, 0), ( 5, 1, 0)],
...,
[(21, 3, 0), (21, 3, 0), (21, 3, 0), ..., (25, 1, 0),
(25, 1, 0), (25, 1, 0)],
[(21, 3, 0), (21, 3, 0), (21, 3, 0), ..., (25, 1, 0),
(25, 1, 0), (25, 1, 0)],
[(21, 3, 0), (21, 3, 0), (21, 3, 0), ..., (25, 1, 0),
(25, 1, 0), (25, 1, 0)]],
dtype=[('f0', 'u1'), ('f1', '<i8'), ('f2', '<u2')])
# Option 2.b Create a histogram
hist = create_histogram_(merged_image)
hist.to_dict()
{('1', '2', '4'): 0.029739583333333333,
('1', '3', '4'): 0.00302734375,
('1', '3', '62'): 0.0020963541666666665,
('2', '2', '4'): 0.029990234375,
('2', '3', '4'): 0.0069140625,
('2', '3', '21'): 0.00012044270833333334,
('3', '1', '4'): 0.0009049479166666666,
('3', '1', '21'): 6.184895833333334e-05,
('3', '2', '4'): 0.015315755208333334,
('3', '2', '21'): 0.018649088541666668,
('3', '3', '4'): 3.255208333333333e-05,
('3', '3', '21'): 0.00029296875,
('4', '1', '4'): 0.008655598958333333,
('4', '1', '21'): 0.00861328125,
('4', '1', '85'): 0.001435546875,
('4', '1', '88'): 0.0019270833333333334,
('4', '2', '4'): 0.007486979166666667,
('4', '2', '21'): 0.00020182291666666667,
('4', '2', '85'): 0.0020963541666666665,
('4', '2', '88'): 0.004651692708333334,
('5', '1', '21'): 0.035494791666666664,
('6', '3', '4'): 0.010615234375,
('6', '3', '62'): 0.006214192708333333,
('6', '4', '4'): 0.006484375,
('6', '4', '62'): 0.0044986979166666665,
('6', '5', '4'): 0.0074251302083333335,
('6', '5', '62'): 0.00078125,
('6', '6', '4'): 0.00025390625,
('7', '3', '4'): 0.014518229166666667,
('7', '3', '21'): 0.005657552083333333,
('7', '4', '4'): 0.009993489583333333,
('7', '4', '21'): 0.0027994791666666667,
('7', '4', '88'): 0.0003938802083333333,
('7', '5', '4'): 0.004117838541666667,
('7', '5', '21'): 0.00027018229166666667,
('7', '6', '4'): 0.0005924479166666667,
('8', '2', '4'): 0.007776692708333333,
('8', '2', '21'): 0.0026041666666666665,
('8', '2', '85'): 0.003994140625,
('8', '2', '88'): 0.008134765625,
('8', '3', '4'): 0.004593098958333333,
('8', '3', '21'): 0.000732421875,
('8', '3', '85'): 0.0032649739583333333,
('8', '3', '88'): 0.00705078125,
('8', '4', '4'): 2.2786458333333334e-05,
('8', '4', '88'): 0.00024739583333333335,
('9', '1', '21'): 0.008873697916666666,
('9', '1', '85'): 0.003404947916666667,
('9', '1', '88'): 0.00193359375,
('9', '2', '85'): 0.013567708333333333,
('9', '2', '88'): 0.011643880208333334,
('10', '1', '21'): 0.038125,
('11', '4', '87'): 0.0003385416666666667,
('11', '5', '87'): 0.00034505208333333333,
('11', '6', '4'): 0.00042317708333333335,
('11', '6', '21'): 0.008740234375,
('11', '6', '87'): 1.953125e-05,
('11', '7', '21'): 3.2552083333333335e-06,
('11', '8', '21'): 0.0020247395833333333,
('11', '9', '21'): 9.440104166666666e-05,
('12', '4', '4'): 0.0005143229166666666,
('12', '4', '21'): 9.114583333333334e-05,
('12', '4', '88'): 0.009567057291666667,
('12', '5', '4'): 0.00021158854166666667,
('12', '5', '21'): 0.008707682291666666,
('12', '5', '88'): 0.0018359375,
('12', '6', '4'): 0.001083984375,
('12', '6', '21'): 0.00291015625,
('12', '7', '21'): 4.557291666666667e-05,
('12', '8', '21'): 0.003203125,
('12', '9', '21'): 0.00012369791666666668,
('13', '2', '85'): 0.013180338541666666,
('13', '2', '88'): 0.0019010416666666668,
('13', '3', '85'): 0.010133463541666667,
('13', '3', '88'): 0.01376953125,
('13', '4', '88'): 0.001015625,
('14', '1', '21'): 0.008395182291666666,
('14', '1', '85'): 0.003544921875,
('14', '1', '88'): 0.0014485677083333334,
('14', '2', '85'): 0.018606770833333335,
('14', '2', '88'): 0.0072591145833333336,
('15', '1', '21'): 0.038125,
('16', '3', '87'): 0.0017057291666666666,
('16', '4', '87'): 0.0038313802083333333,
('16', '5', '11'): 1.6276041666666666e-05,
('16', '5', '87'): 0.0052669270833333335,
('16', '6', '11'): 0.000859375,
('16', '6', '21'): 0.007542317708333334,
('16', '6', '87'): 0.00032877604166666667,
('16', '7', '11'): 0.0016634114583333334,
('16', '7', '21'): 0.00021484375,
('16', '8', '11'): 7.161458333333333e-05,
('16', '8', '21'): 0.001474609375,
('17', '2', '66'): 0.0033333333333333335,
('17', '3', '19'): 0.00011393229166666667,
('17', '3', '21'): 0.00033203125,
('17', '3', '66'): 0.009674479166666666,
('17', '4', '19'): 3.580729166666667e-05,
('17', '4', '21'): 0.00034505208333333333,
('17', '4', '66'): 0.00017903645833333332,
('17', '4', '88'): 0.0006022135416666667,
('17', '5', '11'): 6.184895833333334e-05,
('17', '5', '19'): 3.2552083333333335e-06,
('17', '5', '21'): 0.004583333333333333,
('17', '5', '66'): 8.138020833333333e-05,
('17', '5', '88'): 0.00011393229166666667,
('17', '6', '11'): 0.002151692708333333,
('17', '7', '11'): 0.0029622395833333332,
('17', '7', '21'): 0.0005338541666666666,
('17', '8', '11'): 0.001959635416666667,
('17', '8', '21'): 0.0022819010416666665,
('17', '9', '11'): 0.000546875,
('18', '2', '21'): 0.001015625,
('18', '2', '66'): 0.026809895833333333,
('18', '2', '85'): 0.0008365885416666667,
('18', '2', '88'): 0.00181640625,
('18', '3', '21'): 0.0035286458333333333,
('18', '3', '66'): 0.001005859375,
('18', '3', '85'): 0.00010416666666666667,
('18', '3', '88'): 0.00111328125,
('18', '4', '66'): 8.138020833333333e-05,
('18', '4', '88'): 3.255208333333333e-05,
('19', '1', '21'): 0.010162760416666666,
('19', '1', '67'): 0.000615234375,
('19', '1', '85'): 0.0007975260416666667,
('19', '1', '88'): 0.0021126302083333335,
('19', '2', '21'): 0.014156901041666666,
('19', '2', '67'): 0.0002669270833333333,
('19', '2', '85'): 0.0022135416666666666,
('19', '2', '88'): 0.004134114583333333,
('20', '1', '21'): 0.038125,
('21', '3', '11'): 0.007161458333333333,
('21', '3', '87'): 0.0057161458333333335,
('21', '4', '11'): 0.010299479166666667,
('21', '4', '87'): 0.0011783854166666668,
('21', '5', '11'): 0.006149088541666667,
('21', '5', '87'): 0.002936197916666667,
('21', '6', '11'): 0.00060546875,
('22', '2', '3'): 0.005546875,
('22', '2', '11'): 6.510416666666667e-06,
('22', '2', '66'): 0.00021484375,
('22', '3', '3'): 0.0037044270833333334,
('22', '3', '11'): 0.005465494791666667,
('22', '3', '19'): 0.0018977864583333334,
('22', '3', '66'): 0.0005891927083333334,
('22', '4', '11'): 0.008958333333333334,
('22', '4', '19'): 0.0015169270833333332,
('22', '4', '21'): 3.580729166666667e-05,
('22', '5', '11'): 0.004593098958333333,
('22', '5', '19'): 0.00017252604166666667,
('22', '5', '21'): 0.000341796875,
('22', '6', '11'): 0.000908203125,
('23', '1', '3'): 6.184895833333334e-05,
('23', '1', '19'): 0.0025911458333333333,
('23', '2', '3'): 0.013990885416666666,
('23', '2', '19'): 0.005843098958333333,
('23', '2', '30'): 0.00064453125,
('23', '2', '66'): 0.00515625,
('23', '2', '67'): 0.004449869791666667,
('24', '1', '19'): 0.0017610677083333332,
('24', '1', '21'): 0.00720703125,
('24', '1', '67'): 0.010319010416666666,
('24', '2', '19'): 0.0002669270833333333,
('24', '2', '21'): 5.5338541666666664e-05,
('24', '2', '30'): 0.002509765625,
('24', '2', '67'): 0.00021809895833333333,
('25', '1', '21'): 0.036188151041666665}
high_level_elements = {
0: Eps_set, # positions
1: Eds_set, # depths
2: Eos_set # objects
}
evaluator = Evaluator(operations, hist, high_level_elements=high_level_elements)
E1 = E("left", "faraway", "room")
E2 = E("center", "near", "printer")
E1_expr = parser.parse_string(E1.value)
HE1 = evaluator.eval(E1_expr)
print("Expression for E1:\n{}".format(E1.value))
print("\nThe parsed expressino for E1 in the postfix notation:\n{}".format(E1_expr))
print("\nHistogram of E1 given the image:\n{}".format(HE1.to_dict()))
print("\nValue of presence for E1:\n{}".format(HE1.sum()))
Expression for E1:
(left,faraway,room)
The parsed expressino for E1 in the postfix notation:
[('left', 'faraway', 'room')]
Histogram of E1 given the image:
{('17', '8', '11'): 0.001959635416666667, ('17', '8', '21'): 0.0022819010416666665, ('12', '7', '21'): 4.557291666666667e-05, ('12', '9', '21'): 0.00012369791666666668, ('16', '8', '11'): 7.161458333333333e-05, ('16', '7', '21'): 0.00021484375, ('16', '7', '11'): 0.0016634114583333334, ('16', '8', '21'): 0.001474609375, ('17', '9', '11'): 0.000546875, ('17', '7', '21'): 0.0005338541666666666, ('12', '8', '21'): 0.003203125, ('11', '8', '21'): 0.0020247395833333333, ('11', '7', '21'): 3.2552083333333335e-06, ('11', '9', '21'): 9.440104166666666e-05, ('17', '7', '11'): 0.0029622395833333332}
Value of presence for E1:
0.017203776041666667
E2_expr = parser.parse_string(E2.value)
HE2 = evaluator.eval(E2_expr)
print("Expression for E2:\n{}".format(E2.value))
print("\nThe parsed expressino for E2 in the postfix notation:\n{}".format(E2_expr))
print("\nHistogram of E2 given the image:\n{}".format(HE2.to_dict()))
print("\nValue of presence for E2:\n{}".format(HE2.sum()))
Expression for E2:
(center,near,printer)
The parsed expressino for E2 in the postfix notation:
[('center', 'near', 'printer')]
Histogram of E2 given the image:
{('18', '2', '66'): 0.026809895833333333, ('17', '2', '66'): 0.0033333333333333335}
Value of presence for E2:
0.030143229166666667
# Plot histogram elements
E1_set = extract_element_set(HE1, 2)
E2_set = extract_element_set(HE2, 2)
E1_image = filter_data(I, merged_image, HE1.elements())
E2_image = filter_data(I, merged_image, HE2.elements())
fig, axes = plt.subplots(1, 3, figsize=(14,20))
axes[0].set_title("Initial Image")
axes[0].imshow(I)
axes[0].get_xaxis().set_visible(False)
axes[0].get_yaxis().set_visible(False)
axes[1].set_title("E1: {}".format(E1.value))
axes[1].imshow(I)
axes[1].imshow(E1_image, alpha=0.8)
axes[1] = plot_position_grid(position_transformer, axes[1], E1_set[0])
axes[1].get_xaxis().set_visible(False)
axes[1].get_yaxis().set_visible(False)
axes[2].set_title("E2: {}".format(E2.value))
axes[2].imshow(I)
axes[2].imshow(E2_image, alpha=0.8)
axes[2] = plot_position_grid(position_transformer, axes[2], E2_set[0])
axes[2].get_xaxis().set_visible(False)
axes[2].get_yaxis().set_visible(False)
plt.show()
# Expression with union
E_union = E1 + E2
# Parsed expression
E_union_expr = parser.parse_string(E_union.value)
# Calculate histogram value
HE_union = evaluator.eval(E_union_expr)
print("Expression for E_union:\n{}".format(E_union))
print("\nThe parsed expression for E_union in the postfix notation:\n{}".format(E_union_expr))
print("\nHistogram of E_union given the image:\n{}".format(HE_union.to_dict()))
print("\nValue of presence for E_union:\n{}".format(HE_union.sum()))
Expression for E_union:
((left,faraway,room)+(center,near,printer))
The parsed expression for E_union in the postfix notation:
[('left', 'faraway', 'room'), ('center', 'near', 'printer'), '+']
Histogram of E_union given the image:
{('17', '8', '11'): 0.001959635416666667, ('17', '8', '21'): 0.0022819010416666665, ('12', '7', '21'): 4.557291666666667e-05, ('12', '9', '21'): 0.00012369791666666668, ('16', '8', '11'): 7.161458333333333e-05, ('16', '7', '21'): 0.00021484375, ('16', '7', '11'): 0.0016634114583333334, ('16', '8', '21'): 0.001474609375, ('17', '9', '11'): 0.000546875, ('17', '7', '21'): 0.0005338541666666666, ('17', '2', '66'): 0.0033333333333333335, ('12', '8', '21'): 0.003203125, ('11', '8', '21'): 0.0020247395833333333, ('11', '7', '21'): 3.2552083333333335e-06, ('11', '9', '21'): 9.440104166666666e-05, ('18', '2', '66'): 0.026809895833333333, ('17', '7', '11'): 0.0029622395833333332}
Value of presence for E_union:
0.047347005208333334
# Extract ids of non-zero elements for each feature
E_result_set = extract_element_set(HE_union, 2)
# Plot elements and result
transformers = (position_transformer, depth_transformer, object_transformer)
titles = ["E1: {}".format(E1), "E2: {}".format(E2), "Result: {}".format(E_union)]
show_operation_result(I, merged_image, "f2", HE1, HE2, HE_union, transformers, titles)
operation_list = [
# set operations
("union", "+", E1 + E2),
("intersection", "*", E1 * E2),
("substraction", "-", E1 - E2), # or exception, or E1.Sub(E2)
# logic operations
("and", "&", E1 & E2), # or E1.And(E2)
("or", "|", E1 | E2), # or E1.Or(E2)
("xor", "^", E1 ^ E2), # or E1.Xor(E2)
("xsubstraction", "Xsub", E1.Xsub(E2)),
]
for op_name, op_sign, op in operation_list:
E_expr = parser.parse_string(op.value)
HE = evaluator.eval(E_expr)
print("{:12}{:^12}{:10}".format("Operation", "Sign", "Result"))
print("{}".format("-"*34))
print("{:12}{:^12}{:.5f}".format(op_name, op_sign, HE.sum()))
E_result_set = extract_element_set(HE, 2)
titles = ["E1: {}".format(E1), "E2: {}".format(E2), "Result: {}".format(op)]
show_operation_result(I, merged_image, "f2", HE1, HE2, HE, transformers, titles)
Operation Sign Result ---------------------------------- union + 0.04735
Operation Sign Result ---------------------------------- intersection * 0.00000
Operation Sign Result ---------------------------------- substraction - 0.01720
Operation Sign Result ---------------------------------- and & 0.01720
Operation Sign Result ---------------------------------- or | 0.04735
Operation Sign Result ---------------------------------- xor ^ 0.03014
Operation Sign Result ---------------------------------- xsubstraction Xsub 0.00000
# Load histograms of images if exist, otherwise transform images to histograms and return
hists = load_nyu_histograms(data)
# Initialize a search engine
search_engine = SearchEngine(hists, parser, evaluator)
TOP_N = 20
# Define your query
query = E("center", "faraway", "door") & E("right", "notfar", "table")
# Retrieve images using the query
ranked_images = search_engine.retrieve(query, topN=TOP_N)
print("Total retrieved images:", len(ranked_images))
ranked_images[:5]
Total retrieved images: 16
[(1432, 0.011907552083333333), (1379, 0.011868489583333332), (272, 0.01115234375), (1386, 0.010013020833333334), (264, 0.00828125)]
# Show top ranked images
show_retrieved_images(
ranked_images,
images=data["images"],
limit=TOP_N,
title="Query: {}".format(query.value)
)
# Take a sample image and its histogram from the list of histograms that were created previously
sample_image_id = hists[4][0]
sample_hist = hists[4][1]
# Show the sample image
I = data["images"][sample_image_id]
plt.imshow(I)
plt.title("Image")
plt.show()
# Retrieve images similar to the sample
ranked_images__sample = search_engine.retrieve(sample_hist, topN=TOP_N)
print("Total retrieved images:", len(ranked_images__sample))
ranked_images__sample[:5]
Total retrieved images: 20
[(4, 0.7920149739583339), (969, 0.22738606770833333), (25, 0.1683170572916667), (797, 0.16732747395833336), (270, 0.16452473958333338)]
# Show top ranked images
show_retrieved_images(
ranked_images__sample,
images=data["images"],
limit=TOP_N,
title="Query: {}".format("Sample Image")
)